<?php
/**
 *   Joomdiet - administrator/components/models/recipefood.php
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2010 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.model');

class JoomdietModelRecipeFood extends JModel
{
    var $_recipe_id; // base id - primary key
    var $_food_id; // primary key
    var $_data;
    var $_recipefood;
    
    function __construct()
    {
        parent::__construct();
        
        try {
            $array = JRequest::getVar('cid', 0, '', 'array');
            $this->setId((int)$array[0]);
        } catch (Exception $e) {
            // do nothing
        }
    }
    
    function _buildQuery() // builds base query
    {
        $query = 'SELECT * FROM #__joomdiet_Recipe_food'.
            ' WHERE id_recipe='.$this->_recipe_id;
        return $query;
    }
    
    function setId($id)
    {
        $this->_recipe_id = $id;
        $this->_data = null;
        $this->_recipefood = null;
    }
    
    function getData()
    {
        if (empty($this->_data)) {
            $query = $this->_buildQuery();
            $this->_data = $this->_getList($query);
            if (empty($this->_data)) $this->_data = array();
        }
        
        return $this->_data;
    }
    
    function loadRecipeFood($food_id)
    {
        if (empty($this->_recipefood) and
            ($this->_recipefood->food_id == $food_id)) {
            $query = 'SELECT * FROM #__joomdiet_Recipe_food'.
                ' WHERE id_recipe='.$this->_recipe_id.' AND'.
                ' id_food='.$this->_food_id;
            $this->_db->setQuery($query);
            $this->_recipefood = $this->_db->loadObject();
        }
        
        return $this->_recipefood;
    }
    
    function insert($recipe_id, $food_id)
    {
        $query = 'INSERT INTO #__joomdiet_Recipe_food'.
            ' (id_recipe, id_food, amount, unit)'.
            ' VALUES ('.$recipe_id.', '.$food_id.', 0, "not implemented yet")';
        $this->_db->setQuery($query);
        $this->_db->query();
    }
    
    function update()
    {
        $query = 'UPDATE #__joomdiet_Recipe_food'.
            'SET amount="'.$this->_foodvariable->amount.'",'.
            ' unit="'.$this->_foodvariable->unit.'"'.
            ' WHERE id_recipe='.$this->_foodvariable->recipe_id.' AND'.
            ' id_food='.$this->_foodvariable->food_id;
       $this->_db->setQuery($query);
       $this->_db->query();
    }
    
    function delete($food_id, $recipe_id)
    {
        $query = 'DELETE FROM #__joomdiet_Recipe_food'.
            ' WHERE id_recipe='.$recipe_id.' AND id_food='.$food_id;
        $this->_db->setQuery($query);
        $this->_db->query();
    }
    
    function apply($recipe_id)
    {
        $query = 'SELECT id_food FROM #__joomdiet_Recipe_food'.
            ' WHERE id_recipe='.$recipe_id;
        $this->_db->setQuery($query);
        echo $this->_db->getQuery();
        $food_ids = $this->_db->loadResultArray();
        foreach ($food_ids as $food_id) {
            $amount = JRequest::getVar('food'.$food_id);
            // unit request
            $update = 'UPDATE #__joomdiet_Recipe_food'.
                ' SET amount='.$amount.
                ' WHERE id_recipe='.$recipe_id.' AND'.
                ' id_food='.$food_id;
            $this->_db->setQuery($update);
            $this->_db->query();
        }
    }
}
?>
